package com.dingpeikeji.quickdeer.modules.priceup.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.modules.priceup.entity.TPriceupHist;
import com.dingpeikeji.quickdeer.modules.priceup.service.TPriceupHistService;

/**
 * 价格干预Controller
 * @author dingpei
 * @version 2017-03-08
 */
@Controller
@RequestMapping(value = "${adminPath}/priceup/tPriceupHist")
public class TPriceupHistController extends BaseController {

	@Autowired
	private TPriceupHistService tPriceupHistService;
	
	@ModelAttribute
	public TPriceupHist get(@RequestParam(required=false) String id) {
		TPriceupHist entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tPriceupHistService.get(id);
		}
		if (entity == null){
			entity = new TPriceupHist();
		}
		return entity;
	}
	
	@RequiresPermissions("priceup:tPriceup:view")
	@RequestMapping(value = {"list", ""})
	public String list(TPriceupHist tPriceupHist, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TPriceupHist> page = tPriceupHistService.findPage(new Page<TPriceupHist>(request, response), tPriceupHist); 
		model.addAttribute("page", page);
		model.addAttribute("tHist", tPriceupHist);
		return "modules/priceup/tPriceupHistList";
	}

	@RequiresPermissions("priceup:tPriceup:view")
	@RequestMapping(value = "form")
	public String form(TPriceupHist tPriceupHist, Model model) {
		model.addAttribute("tPriceupHist", tPriceupHist);
		return "modules/priceup/tPriceupHistForm";
	}

	@RequiresPermissions("priceup:tPriceup:edit")
	@RequestMapping(value = "save")
	public String save(TPriceupHist tPriceupHist, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tPriceupHist)){
			return form(tPriceupHist, model);
		}
		tPriceupHistService.save(tPriceupHist);
		addMessage(redirectAttributes, "保存价格干预成功");
		return "redirect:"+Global.getAdminPath()+"/priceup/tPriceupHist/?repage";
	}
	
	@RequiresPermissions("priceup:tPriceup:edit")
	@RequestMapping(value = "delete")
	public String delete(TPriceupHist tPriceupHist, RedirectAttributes redirectAttributes) {
		tPriceupHistService.delete(tPriceupHist);
		addMessage(redirectAttributes, "删除价格干预成功");
		return "redirect:"+Global.getAdminPath()+"/priceup/tPriceupHist/?repage";
	}

}